package tomasulo.ui;

import java.awt.Component;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class Chooser {

	JFileChooser chooser = null;
	Component parent = null;
	String type = null;
	
	
	public Chooser(Component parent, String postfix) {
		this.type = postfix;
		chooser = new JFileChooser();
		chooser.setFileFilter(new FileFilter() {

			@Override
			public boolean accept(File arg0) {
				if (arg0.isDirectory()) {
					return true;
				} else if (arg0.getName().endsWith("." + type)) {
					return true;
				} else {
					return false;
				}
			}

			@Override
			public String getDescription() {
				return "*." + type;
			}
			
		});
	}
	
	public File getFile() {
		chooser.setSelectedFile(null);
		int ret = chooser.showOpenDialog(parent);
		if (ret == JFileChooser.APPROVE_OPTION) {
			return chooser.getSelectedFile();
		} else {
			return null;
		}
	}
	
}
